from src.common.logger import getLogger
from src.agentic.agent.AgentTools import AgentTools
from src.agentic.config.LanguageModel import LanguageModel
from src.agentic.agent.multi.SupervisorAgentic import SupervisorAgentic
from src.agentic.agent.multi.HierarchicalAgentic import HierarchicalAgentic
from src.agentic.agent.multi.CollaborationAgentic import CollaborationAgentic
from src.modules.memory.service import HistoryRecordService, MemoryDetailService

logger = getLogger()

def invoke_agent(args):

    HistoryRecordService.insert_history_memory(args)

    base_url = "http://localhost:11434"
    llm_model = LanguageModel("qwen3:4b", base_url, None)
    llm = llm_model.new_llm_model()

    agent_tools = AgentTools()
    tools = agent_tools.get_execute_tools()

    query = args.get("query")
    logger.info(f"invoke_agent query: {query}")

    agent = None
    agent_pattern = args.get("pattern")
    if agent_pattern == "Supervisor":
        agent = SupervisorAgentic(llm, tools)
    elif agent_pattern == "Collaboration":
        agent = CollaborationAgentic(llm, tools)
    elif agent_pattern == "Hierarchical":
        agent = HierarchicalAgentic(llm, tools)
    response = agent.invoke(query)

    MemoryDetailService.insert_memory_detail_ai(args, response)

    logger.info(f"invoke_agent response len: {len(response)}")
    return response
